﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace BU
{
    /**
     * 
     * Manager lié aux Produits
     * 
     **/
    public class ProductManager : EntityManager
    {
        /**
         * 
         * Récupère un produit selon un id
         * 
         */
        public Product getOne(int id)
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();
            DataRow productRow = provider.findOne(id);

            if (productRow == null)
                return null;

            // Construction du produit
            Product product = new Product();
            product.ID = (int)productRow["id_product"];
            product.Name = (string)productRow["name"];
            product.Description = (string)productRow["description"];
            product.Price = (decimal)productRow["price"];
            product.Stock = (int)productRow["stock"];
            product.CategoryID = (int)productRow["category_id_category"];

            return product;
        }

        /**
         * 
         * Met à jour un produit
         * 
         */
        public bool save(int id, string name, string description, string price, string stock)
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();

            DataTable productDT = new DataTable();
            productDT.Columns.Add("id", typeof(int));
            productDT.Columns.Add("name", typeof(string));
            productDT.Columns.Add("description", typeof(string));
            productDT.Columns.Add("price", typeof(decimal));
            productDT.Columns.Add("stock", typeof(int));

            productDT.Rows.Add(id, name, description, price, stock);

            provider.save(productDT); // chope bien la modif

            return true;
        }

        /**
         * 
         * Ajoute ou modifie un nouveau produit
         * 
         */
        public bool save(int id, string name, string description, string price, string stock, int category_id)
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();

            DataTable productDT = new DataTable();
            productDT.Columns.Add("id", typeof(int));
            productDT.Columns.Add("name", typeof(string));
            productDT.Columns.Add("description", typeof(string));
            productDT.Columns.Add("price", typeof(decimal));
            productDT.Columns.Add("stock", typeof(int));
            productDT.Columns.Add("category_id_category", typeof(int));

            productDT.Rows.Add(id, name, description, price, stock, category_id);

            provider.save(productDT);

            return true;
        }

        /**
         * 
         * Supprime un produit
         * 
         */
        public bool delete(int id)
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();
            provider.delete(id);
            return true;
        }

        /**
         * 
         * Récupère tous les produits
         * 
         **/
        public List<Product> getAll()
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();
            DataTable dt = provider.findAll();

            List<Product> products = new List<Product>();

            foreach (DataRow row in dt.Rows)
            {
                Product product = new Product();
                product.ID = (int)row["id_product"];
                product.Name = (string)row["name"];
                product.Description = (string)row["description"];
                product.Price = (decimal)row["price"];
                product.Stock = (int)row["stock"];
                products.Add(product);
            }

            return products;
        }

        public List<Product> getProductsFromCategory(int category_id)
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();
            DataTable dt = provider.findFromCategory(category_id);

            List<Product> products = new List<Product>();

            foreach (DataRow row in dt.Rows)
            {
                Product product = new Product();
                product.ID = (int)row["id_product"];
                product.Name = (string)row["name"];
                product.Description = (string)row["description"];
                product.Price = (decimal)row["price"];
                product.Stock = (int)row["stock"];
                products.Add(product);
            }

            return products;
        }

        public List<Product> getLastProducts()
        {
            DAL.ProductDataProvider provider = new DAL.ProductDataProvider();
            DataTable dt = provider.getLastProducts();

            List<Product> products = new List<Product>();

            foreach (DataRow row in dt.Rows)
            {
                Product product = new Product();
                product.ID = (int)row["id_product"];
                product.Name = (string)row["name"];
                product.Description = (string)row["description"];
                product.Price = (decimal)row["price"];
                product.Stock = (int)row["stock"];
                products.Add(product);
            }

            return products;
        }
    }
}
